package com.zkingsoft.zkingbase.activities;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.zkingsoft.zkingbase.R;
import com.zkingsoft.zkingbase.base.BaseActivity;
import com.zkingsoft.zkingbase.bean.resp.BaseResp;
import com.zkingsoft.zkingbase.constant.Constant;
import com.zkingsoft.zkingbase.manager.JPushManager;
import com.zkingsoft.zkingbase.manager.OkHttpManager;
import com.zkingsoft.zkingbase.utils.PreferenceUtils;

import java.util.Set;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jpush.android.api.TagAliasCallback;

public class PushActivity extends BaseActivity
{

    @BindView(R.id.btn_jiguang_push)
    Button btnJiguangPush;
    @BindView(R.id.tv_jpush_alias)
    EditText tvJpushAlias;
    @BindView(R.id.btn_jpush_add_alias)
    Button btnJpushAddAlias;
    @BindView(R.id.btn_jiguang_alias_push)
    Button btnJiguangAliasPush;

    private OkHttpManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        manager = OkHttpManager.getInstance();
    }

    @Override
    public int getContentView()
    {
        return R.layout.activity_push;
    }

    @OnClick(value = {R.id.btn_jiguang_push, R.id.btn_jpush_add_alias, R.id.btn_jiguang_alias_push})
    public void click(View v)
    {
        switch (v.getId())
        {
            case R.id.btn_jiguang_push:
                manager.doPost(Constant.WEB_BASE + "/jpush/notifyOne", null,new OkHttpManager.ResultCallback<BaseResp>()
                {
                    @Override
                    public void onError(String simpleMsg, Exception e)
                    {
                        Toast.makeText(PushActivity.this, simpleMsg, Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onSuccess(BaseResp response)
                    {
                        Toast.makeText(PushActivity.this, response.info, Toast.LENGTH_SHORT).show();
                    }
                });
                break;

            case R.id.btn_jpush_add_alias:
                String alias = null;
                if(TextUtils.isEmpty(tvJpushAlias.getText()))
                {
                    Toast.makeText(PushActivity.this, "不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }else
                {
                    alias = tvJpushAlias.getText().toString().trim();
                }
                if((Boolean) PreferenceUtils.get("jpush_alias_1", false))
                {
                    Toast.makeText(PushActivity.this, "您已经设置过了", Toast.LENGTH_SHORT).show();
                }
                JPushManager.getInstance().setAlias(alias, this, new TagAliasCallback()
                {
                    @Override
                    public void gotResult(int code, String alias, Set<String> tags)
                    {
                        switch (code)
                        {
                            case 0:
                                //成功
                                PreferenceUtils.put("jpush_alias_1", true);
                                Toast.makeText(PushActivity.this, "设置成功", Toast.LENGTH_SHORT).show();
                                break;
                            case 6002:
                                //失败
                                // 延迟 60 秒来调用 Handler 设置别名
                                Toast.makeText(PushActivity.this, "设置alias失败，请稍后再试", Toast.LENGTH_SHORT).show();
                                break;
                            default:
                        }
                    }
                });

                break;

            case R.id.btn_jiguang_alias_push:
                if(!(Boolean)PreferenceUtils.get("jpush_alias_1", false))
                {
                    Toast.makeText(PushActivity.this, "需要先设置alias", Toast.LENGTH_SHORT).show();
                }else
                {
                    manager.doPost(Constant.WEB_BASE + "/jpush/notifySingleOne", null, new OkHttpManager.ResultCallback<BaseResp>()
                    {
                        @Override
                        public void onError(String simpleMsg, Exception e)
                        {

                        }

                        @Override
                        public void onSuccess(BaseResp response)
                        {
                            Toast.makeText(PushActivity.this, response.info, Toast.LENGTH_SHORT).show();
                        }
                    });
                }

                break;
        }
    }

    private final TagAliasCallback mAliasCallback = new TagAliasCallback() {
        @Override
        public void gotResult(int code, String alias, Set<String> tags) {

        }
    };
}
